(function($) {
	jQuery.fn.ajaxPictureUpload = function(options) {
		var options = $.extend( {}, jQuery.fn.ajaxPictureUpload.defaults,
				options);
		var ajaxUpload = function ajaxUpload() {
			var uploadValue = $("#" + options.uploadId).attr("value");
			var date = new Date().getTime();
			if (!chk_ext(uploadValue)) {
				alert("不支持此格式！");
				return false;
			} else {
				$.ajaxFileUpload( {
					url : options.url + '?dateTime=' + date,
					secureuri : false,
					fileElementId : options.uploadId,
					dataType : 'json',
					success : function(data, status) {
						$("#" + options.imgId).attr("src", data.tempUrl);
						$("#" + options.hiddenId).attr("value", data.tempUrl);
					},
					error : function(data, status, e) {
					}
				});
			}
			$("#" + options.uploadId).change(ajaxUpload);
		};

		function ltrim(s) {
			return s.replace(/^\s*/, "");
		}

		// 去右空格
		function rtrim(s) {
			return s.replace(/\s*$/, "");
		}

		// 去左右空格
		function trim(s) {
			return rtrim(ltrim(s));
		}

		// 获取文件扩展名
		function get_ext(f_path) {
			var ext = '';

			if (f_path != null && trim(f_path).length > 0) {
				f_path = trim(f_path);
				ext = f_path.substring(f_path.lastIndexOf(".") + 1,
						f_path.length);
			}

			return ext;
		}

		// 验证文件扩展名
		function chk_ext(f_path) {
			var ext = get_ext(f_path);

			// 根据需求定制
			var accept_ext = new Array("BMP", "JPEG", "GIF", "PSD", "PNG",
					"TIFF", "TGA", "EPS", "JPG");

			var flag = false;

			if (ext != '') {
				for ( var i = 0; i < accept_ext.length; i++) {
					if (ext.toUpperCase() == accept_ext[i])
						flag = true;
				}
			}
			return flag;
		}
		$("#" + options.uploadId).change(ajaxUpload);
	};
	// 默认值
	jQuery.fn.ajaxPictureUpload.defaults = {
		uploadId : "upload_1",
		imgId : "imgshow_1",
		hiddenId : "hidden_1",
		url : "/meimei/json/upload.action"
	}
})(jQuery);